package ccgl.com.server;

import java.sql.SQLException;
import java.util.Map;

import ccgl.com.dao.DdglDao;
import ccgl.com.dao.DdxxDao;
import ccgl.com.model.MDd;
import ccgl.com.model.MDdSearchItem;
import ccgl.com.model.MDdxx;
import ccgl.com.util.UUIDUitl;

/**
 * 货物管理服务接口
 * 
 * @author Administrator
 * 
 */
public class DdglServer {

	DdglDao dao = new DdglDao();

	/**
	 * 添加
	 * @param dd
	 * @return
	 * @throws SQLException 
	 */
	public String addDdxx(MDd dd) throws SQLException {
		dd.setId(UUIDUitl.getNewId());
		if (dao.addDdxx(dd)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	public String updateDdxx(MDd dd) throws SQLException {
		if (dao.updateDdxx(dd)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	public boolean exists(String ddh, String userid) throws SQLException {
		return dao.exists(ddh, userid);
	}

	public Map getDdList(int star, int end, String userid) throws SQLException {
		return dao.getDdList(star, end, userid);
	}
	
	public Map getDdListWithParams(MDdSearchItem item, int star, int end, String userid) throws SQLException {
		String sql = "SELECT * FROM ddb t where userid='"+userid+"'";
		String countSql = "select count(*) from ddb t where userid='"+userid+"'";
		
		if(item.getDdsjStar() != null) {
			sql += " and DATEDIFF('" + item.getDdsjStar() + "' , t.ddsj) < 0";
			countSql += " and DATEDIFF('" + item.getDdsjStar() + "' , t.ddsj) < 0";
		}
		
		if(item.getDdsjEnd() != null) {
			sql += " and DATEDIFF('" + item.getDdsjEnd() + "' , t.ddsj) > 0";
			countSql += " and DATEDIFF('" + item.getDdsjEnd() + "' , t.ddsj) > 0";
		}
		
		if(item.getDdh() != null) {
			sql += " and t.ddh like '%" + item.getDdh() + "%'";
			countSql += " and t.ddh like '%" + item.getDdh() + "%'";
		}
		
		if(item.getKhxm() != null) {
			sql += " and t.khxm like '%" + item.getKhxm() + "%'";
			countSql += " and t.khxm like '%" + item.getKhxm() + "%'";
		}
		
		
		sql +=" LIMIT " + star + "," + end;
		
		return dao.getDdList(sql, countSql);
	}

	public boolean deleteDdxxs(String ids) throws SQLException {
		String[] idArr = ids.split("-");
		String idStr = "";
		for(int i =0;i<idArr.length;i++){
			if(i==(idArr.length-1) ){
				idStr += "'" + idArr[i] + "'";
			}else{
				idStr += "'" + idArr[i] + "',";
			}
		}
		return dao.deleteDdxxs(idStr);
	}

	public Map getDdxxList(int star, int end, String ddid) throws SQLException {
		DdxxDao  dao = new DdxxDao();
		return dao.getDdxxList(star, end, ddid);
	}

	public boolean saveOrUpdateDdxxxx(MDdxx ddxx) throws SQLException {
		if(ddxx.getId() == null || ddxx.getId().trim().length() == 0) {
			ddxx.setId(UUIDUitl.getNewId());
			return dao.saveDdxxxx(ddxx);
		} else {
			return dao.updateDdxxxx(ddxx);
		}
	}

	public boolean deleteDdxxxx(String ids) throws SQLException {
		String[] idArr = ids.split("-");
		String idStr = "";
		for(int i =0;i<idArr.length;i++){
			if(i==(idArr.length-1) ){
				idStr += "'" + idArr[i] + "'";
			}else{
				idStr += "'" + idArr[i] + "',";
			}
		}
		return dao.deleteDdxxxx(idStr);
	}
}
